import request from "@/axios"
import Common from "@/common/global"



export default ({
  namespaced:true,
  state: {
    DiscussData:[],
    DiscussState:false,
    DiscussInfo:{pageNo:1,pageSize:5,length:0,total:0},
   
    // 评论的文章信息 || 回复某人的信息
    DiscussedInfo:null,
    
    //回复窗口
    ReplyState:false,
    ReplyDiscussInfo:null,
    ReplyInfo:{pageNo:1,pageSize:3,length:0,total:0},
    //回复信息
    ReplyData:[],
  },
  getters: {},
  mutations: {
     //评论窗口 关闭 打开
     DISCUSSON:(state,data)=>{
      state.DiscussState=true
      state.DiscussedInfo=data
    },
    DISCUSSDOWN:state=>{
      state.DiscussState=false
      state.DiscussedInfo=null
    },

    //回复组件 关闭 打开
    REPLYON:(state,data) => {
      state.ReplyState = true
      state.ReplyInfo = {pageNo:1,pageSize:3,length:0,total:0}
      state.ReplyDiscussInfo = data
    },
    REPLYDOWN:state =>{
      state.ReplyState = false 
      state.ReplyData = []
      state.ReplyDiscussInfo = null
    },
    
    // 设置评论
    SETDISCUSS:(state,{records,total}) => {
       
      state.DiscussData = []
      //初始化内容
      records.forEach((item,index)=>{
			    item.content = Common.initContent(item.content)
          item.deleted = 1 //出场动画 1 -> 0
          if(item.reply)
          item.reply.forEach(element => element.content = Common.initContent(element.content) )
          state.DiscussData.push(Object.assign({},item))
			})


      state.DiscussInfo.total=total
      state.DiscussInfo.length=state.DiscussData.length

    },
    //删除评论(本地假装删除评论)
    DELETEDISCUSS:(state,data) =>{
      data.deleted = 2
      state.DiscussInfo.total=state.DiscussInfo.total - 1
      state.DiscussInfo.length=state.DiscussData.length
    },
    //过滤评论(本地真正删除评论的地方)
    FILTERDISCUSS:(state) =>{
      state.DiscussData = state.DiscussData.filter(item => item.deleted != 2)
    },
    //增加评论
    ADDDISCUSS:(state,data)=>{   
       data.content = Common.initContent(data.content)
       data.deleted = 1
       data.reply = []
       state.DiscussData.unshift(Object.assign({},data))
       state.DiscussInfo.length=state.DiscussData.length
    },

    //设置回复
    SETREPLY:(state,{records,total}) => {
       state.ReplyData = []
       //初始化内容
       records.forEach((item)=>{
			    item.content = Common.initContent(item.content)
          state.ReplyData.push(Object.assign({},item))
			})

      state.ReplyInfo.total=total
      state.ReplyInfo.length=state.DiscussData.length

    },
    //增加回复
    ADDREPLY:(state,data) =>{
        data.content = Common.initContent(data.content)
        state.DiscussData.forEach(item => {
          if(item.id == data.discussId)
           {
             item.replyCount  += 1
             data.deleted = 1
             item.reply.unshift(data)
             let timer = setTimeout(() => {
              data.deleted = 0
              clearTimeout(timer)
             }, 100);
           }
        })
        let newData = {...data}
        newData.deleted = 1
        state.ReplyData.unshift(newData)
        let timer = setTimeout(() => {
          newData.deleted = 0
          clearTimeout(timer)
         }, 100);
         state.ReplyInfo.length=state.ReplyInfo.length
    },
    //减少回复
    DELETEREPLY:(state,data) =>{
      state.DiscussData.forEach(item => {
        if(item.id == data.discussId)
         {
           item.replyCount -= 1
           let index = item.reply.findIndex(element => element.id == data.id)      
           if(index != -1)
           item.reply[index].deleted = 1
           let timer = setTimeout(() => {
            item.reply.splice(index,1)
            clearTimeout(timer)
           }, 500);
         }
      })

      let index = state.ReplyData.findIndex(element => element.id == data.id)

      if(index != -1){
        state.ReplyData[index].deleted = 1
      }
      let timer = setTimeout(() => {
        state.ReplyData.splice(index,1)
        clearTimeout(timer)
       }, 100);
      
       state.ReplyInfo.total=state.ReplyInfo.total - 1
       state.ReplyInfo.length=state.ReplyInfo.length
    },
    //刷新回复
    REFRESHREPLY:state =>{
      state.ReplyData = []
      state.ReplyInfo ={pageNo:1,pageSize:3,length:0,total:0}
    },

    //刷新
    REFRESH:state=>{
       state.DiscussData=[]
       state.DiscussState=false
       state.DiscussInfo ={pageNo:1,pageSize:5,length:0,total:0}
       state.DiscussedInfo = null
    }

    //
  },
  actions:{
    //得到评论
    async getDiscuss(context,articleId){
       let {pageNo,pageSize} = context.state.DiscussInfo 
        try{
          let res =await request.get("/api/discuss/get",{params:{pageNo,pageSize,articleId}})
          if(res.data.code == 200) context.commit('SETDISCUSS',res.data.data)
          return res.data
      }catch (error){ return {message:error,code:400} }
    },

    // 增加评论
    async addDiscuss(context,discuss){

        try{
             let res =await request.post("/api/discuss/add",discuss)
             if(res.data.code == 200){
               context.commit('ADDDISCUSS',res.data.data)            
               context.commit('DISCUSSDOWN')
             }
             return res.data
        }catch (error){ return {message:error,code:400} }
    },
    async Judge(context,discuss){ //删除这一页唯一一个时，跳转到上一页
       if(context.state.DiscussData.length == 1 && context.state.DiscussInfo.pageNo > 1){
         context.state.DiscussInfo.pageNo -= 1
         await context.dispatch('getDiscuss',discuss.articleId)
       }
    },
    async JudgeReply(context,reply){ //删除这一页唯一一个时，跳转到上一页
      if(context.state.ReplyInfo.length == 1 && context.state.ReplyInfo.pageNo > 1){
         context.state.ReplyInfo.pageNo -= 1
        await context.dispatch('getReply',reply.discussId)
      }
   },
    //删除评论
    async deleteDiscuss(context,discuss){
      try{
         let res =await request.post("/api/discuss/delete",discuss)  
         if(res.data.code == 200)  {
           context.commit('DELETEDISCUSS',discuss)
           await  context.dispatch('Judge',discuss)
         }
         return res.data
       }catch (error){ return {message:error,code:400} }
    },

    //刷新评论
    async refresh(context,articleid){
       context.commit('REFRESH')
       return await  context.dispatch('getDiscuss',articleid)
    },
    //得到回复
    async getReply(context,discussId){
      let {pageNo,pageSize} = context.state.ReplyInfo 
      try{
        let res =await request.get("/api/discuss/getReply",{params:{discussId,pageNo,pageSize}})
        if(res.data.code == 200 )
          context.commit('SETREPLY',res.data.data)
        return res.data
      }catch (error){ return {message:error,code:400} }
    },

    //增加回复
    async addReply(context,reply){
      try{
        let res =await request.post("/api/discuss/addReply",reply)
     
        if(res.data.code == 200){
          context.commit('ADDREPLY',res.data.data)        
          context.commit('DISCUSSDOWN')
        }
        return res.data
      }catch (error){ return {message:error,code:400} }
    },
    //删除回复
    async deleteReply(context,reply){
      try{
        let res =await request.post("/api/discuss/deleteReply",reply)
        if(res.data.code == 200){
          context.commit('DELETEREPLY',reply)
          await  context.dispatch('JudgeReply',reply)
        }
        return res.data
      }catch (error){ return {message:error,code:400} }
    }

  },
})